﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Globalization;
using System.Reflection;
using AkazoNobel.Scheduler.Core;
using AkazoNobel.Scheduler.StoreMasterData.Common;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess.Model;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess.Service;
using AkazoNobel.Ultimus.Extension;
using Dapper;
using Quartz;
using Ultimus.OC;
using Ultimus.WFServer;

namespace AkazoNobel.Scheduler.StoreMasterData.Tasks
{
    public class CustomerDataSync : BaseTask
    {
        public override void ExecuteTask(IJobExecutionContext context)
        {
            var dealers = CustomerService.GetModifiedList();
            //foreach (var dealer in dealers)
            //{
            //    var stores = SmdStoreService.GetByDealerId(dealer.DealerID); //该经销商下的所有门店
            //    foreach (var store in stores)
            //    {
            //        var variables = new List<Variable>();
            //        var applicationNo = "";
            //        const string winId = "d30.intra/svc_dpcna_mdc";

            //        //流程执行人
            //        var salesOffice = CustomerService.GetSalesOffice(dealer.CityName);
            //        if (salesOffice == null)
            //        {
            //            continue;
            //        }
            //        variables.AddRange(MdcRecipientService.SetStoreDataChangeRecipient(salesOffice.Region, salesOffice.Province, "Contract"));

            //        variables.Add("BlankVlue", store.StoreID);
            //        variables.Add("Actions", "ChangeContract");
            //        variables.Add("AutoBusinessKey", "DataChange");

            //        var incident = TaskHelper.NewIncident(winId, "Store Data Change", "合同信息变更 / 经销商变更", variables,
            //            out applicationNo);

            //        if (incident == 0)
            //        {
            //            Logger.Debug("流程发起失败！经销商变更：" + dealer.Name);
            //            continue;
            //        }

            //        using (var conn = SqlConnectionHelper.GetConnection(Constant.MDC_DB_NAME))
            //        {
            //            var trans = conn.BeginTransaction();
            //            try
            //            {
            //                TaskHelper.SetAssessment(winId, "Store Data Change", incident, "Fill in the request", "DataChange", 
            //                    conn, trans);
            //                trans.Commit();
            //            }
            //            catch (Exception ex)
            //            {
            //                trans.Rollback();

            //                Logger.Debug("写入业务数据时失败！");

            //                //业务数据写入失败，需要删除发起的流程实例
            //                TaskHelper.DeleteIncident("Store Data Change", incident, winId);
            //            }
            //        }
            //    }
            //}
        }
    }
}
